﻿using System;
using System.Linq;
using Nop.Core.Data;
using Nop.Core.Domain.Tradings;

namespace Nop.Services.Tradings
{
    public class TradingService:ITradingService
    {
        private readonly IRepository<Trading> _tradingRepository;

        private readonly IRepository<TradingId> _tradingIdRepository;

        public TradingService(IRepository<Trading> tradingRepository, IRepository<TradingId> tradingIdRepository)
        {
            _tradingRepository = tradingRepository;
            _tradingIdRepository = tradingIdRepository;
        }

        public Trading GenerateNewTrading()
        {
            var tId = new TradingId();
           _tradingIdRepository.Insert(tId);

            var tradingId = tId.Id;

            var t = new Trading()
                        {
                            Created = DateTime.Now,
                            TradingId = tradingId + 200000,
                        };
            _tradingRepository.Insert(t);
            return t;
        }

        public void Update(Trading trading)
        {
            _tradingRepository.Update(trading);
        }

        public Trading GetTrading(int tradingId)
        {
            var query = from t in _tradingRepository.Table
                        where t.TradingId == tradingId
                        select t;
            return query.SingleOrDefault();
        }
    }
}